<!DOCTYPE html>
<html>
<head>
    <title>Electromagnetic Field Diagram</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <canvas id="physics-canvas" width="450" height="500"></canvas>
    <script>
        const canvas = document.getElementById('physics-canvas');
        const ctx = canvas.getContext('2d');

        // Define parameters
        const cx = canvas.width / 2;
        const cy = 225; // Center Y, leaving space for text at the bottom
        const R = 150;  // Radius of the large cylinder
        const r = 50;   // Radius of the hole
        const d = 80;   // Distance between centers

        // 1. Draw the shaded area (hollow cylinder cross-section)
        ctx.fillStyle = '#EAEAEA'; // Light gray fill
        ctx.beginPath();
        // Outer circle path (clockwise)
        ctx.arc(cx, cy, R, 0, 2 * Math.PI, false);
        // Inner circle path (counter-clockwise to create a hole)
        ctx.arc(cx + d, cy, r, 0, 2 * Math.PI, true);
        ctx.fill();

        // 2. Draw the outlines
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 2;
        // Large circle outline
        ctx.beginPath();
        ctx.arc(cx, cy, R, 0, 2 * Math.PI);
        ctx.stroke();
        // Small circle (hole) outline
        ctx.beginPath();
        ctx.arc(cx + d, cy, r, 0, 2 * Math.PI);
        ctx.stroke();

        // 3. Draw center points and labels O, O'
        ctx.fillStyle = 'black';
        // Point O
        ctx.beginPath();
        ctx.arc(cx, cy, 3, 0, 2 * Math.PI);
        ctx.fill();
        // Point O'
        ctx.beginPath();
        ctx.arc(cx + d, cy, 3, 0, 2 * Math.PI);
        ctx.fill();

        // Labels for centers
        ctx.font = "italic 22px Times New Roman";
        ctx.textAlign = 'right';
        ctx.textBaseline = 'middle';
        ctx.fillText('O', cx - 10, cy);
        
        ctx.textAlign = 'left';
        ctx.textBaseline = 'bottom';
        ctx.fillText("O'", cx + d + 5, cy - 8);

        // 4. Draw the dimension 'd'
        ctx.lineWidth = 1.5;
        const d_line_y = cy - 90;
        const arrowSize = 6;

        // Vertical projection lines
        ctx.beginPath();
        ctx.moveTo(cx, cy);
        ctx.lineTo(cx, d_line_y);
        ctx.moveTo(cx + d, cy);
        ctx.lineTo(cx + d, d_line_y);
        ctx.stroke();
        
        // Horizontal line with arrows
        ctx.beginPath();
        ctx.moveTo(cx, d_line_y);
        ctx.lineTo(cx + d, d_line_y);
        // Left arrow
        ctx.moveTo(cx + arrowSize, d_line_y - arrowSize);
        ctx.lineTo(cx, d_line_y);
        ctx.lineTo(cx + arrowSize, d_line_y + arrowSize);
        // Right arrow
        ctx.moveTo(cx + d - arrowSize, d_line_y - arrowSize);
        ctx.lineTo(cx + d, d_line_y);
        ctx.lineTo(cx + d - arrowSize, d_line_y + arrowSize);
        ctx.stroke();

        // Label 'd'
        ctx.font = "italic 22px Times New Roman";
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.fillText('d', cx + d / 2, d_line_y - 5);

        // 5. Draw the horizontal line connecting centers
        ctx.beginPath();
        ctx.moveTo(cx, cy);
        ctx.lineTo(cx + d, cy);
        ctx.stroke();
        
        // 6. Draw radius R and its label
        ctx.lineWidth = 2;
        const angleR = 225 * Math.PI / 180; // ~7 o'clock position
        const endRx = cx + R * Math.cos(angleR);
        const endRy = cy + R * Math.sin(angleR);
        ctx.beginPath();
        ctx.moveTo(cx, cy);
        ctx.lineTo(endRx, endRy);
        ctx.stroke();
        
        // Label 'R'
        ctx.save();
        ctx.font = "italic 22px Times New Roman";
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        const labelRx = cx + R * 0.55 * Math.cos(angleR) - 15 * Math.sin(angleR);
        const labelRy = cy + R * 0.55 * Math.sin(angleR) + 15 * Math.cos(angleR);
        ctx.fillText('R', labelRx, labelRy);
        ctx.restore();

        // 7. Draw radius r and its label
        const angle_r = 315 * Math.PI / 180; // ~4 o'clock position
        const start_rx = cx + d;
        const start_ry = cy;
        const end_rx = start_rx + r * Math.cos(angle_r);
        const end_ry = start_ry + r * Math.sin(angle_r);
        ctx.beginPath();
        ctx.moveTo(start_rx, start_ry);
        ctx.lineTo(end_rx, end_ry);
        ctx.stroke();
        
        // Label 'r'
        ctx.save();
        ctx.font = "italic 22px Times New Roman";
        ctx.textAlign = 'left';
        ctx.textBaseline = 'top';
        const label_rx = start_rx + r * 0.6 * Math.cos(angle_r) - 10 * Math.sin(angle_r);
        const label_ry = start_ry + r * 0.6 * Math.sin(angle_r) + 10 * Math.cos(angle_r);
        ctx.fillText('r', label_rx, label_ry);
        ctx.restore();
        
        // 8. Draw the bottom text label
        ctx.fillStyle = 'black';
        ctx.font = '24px "SimSun"'; // Use a font that supports Chinese characters
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.fillText('习题 8-26 图', canvas.width / 2, canvas.height - 10);
    </script>
</body>
</html>